package com.beagle.njddapp.ui.presenter;

import android.util.Log;

import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BaseApplication;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.CompanyBean;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.bean.UploadBean;
import com.beagle.njddapp.bean.UserInfoBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.event.UserInfoUpdateEvent;
import com.beagle.njddapp.net.BaseHttpListResponse;
import com.beagle.njddapp.net.BaseHttpResponse;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.UserInfoView;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;


public class UserInfoPresenter extends BasePresenter<UserInfoView> {
    private ApiService apiService;
    private NjOwnerApiService njOwnerApiService;

    public UserInfoPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        apiService = retrofit.create(ApiService.class);
        njOwnerApiService = retrofit.create(NjOwnerApiService.class);

    }

    public void Update(UserInfoBean userInfoBean) {
        Gson gson = new Gson();
        String strEntity = gson.toJson(userInfoBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        apiService.updateUserInfo(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver() {
            @Override
            public void success(Object o) {
                UserService.getInstance().setUserInfoBean(userInfoBean);
                EventBus.getDefault().post(new UserInfoUpdateEvent());
                getView().success();
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }




    public void getCompany() {
        apiService.companys().compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseHttpListResponse<CompanyBean>>() {


            @Override
            public void success(BaseHttpListResponse<CompanyBean> companyBeanBaseHttpListResponse) {
                if (companyBeanBaseHttpListResponse.data!=null){
                    getView().showCampany(companyBeanBaseHttpListResponse.data);
                }else {
                    getView().onError("");
                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }



    public void upload(String path){
        Luban.with(BaseApplication.getInstance().getApplicationContext())
                .load(path)// 传人要压缩的图片列表
                .ignoreBy(100)                                  // 忽略不压缩图片的大小
                .setTargetDir(BaseApplication.getInstance().getCacheDir().getAbsolutePath())                        // 设置压缩后文件存储位置
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {

                    }
                    @Override
                    public void onSuccess(File file) {
                        String name=path.substring(path.lastIndexOf("/")+1,path.length()-1);
                        LogUtils.d(name);
                        RequestBody body=RequestBody.create(MediaType.parse("multipart/form-data"),file);//表单类型
                        MultipartBody.Builder builder = new MultipartBody.Builder()
                                .setType(MultipartBody.FORM);
                        builder.addFormDataPart("file",file.getName(),body); //添加图片数据，body创建的请求体
                        List<MultipartBody.Part> parts=builder.build().parts();
                        apiService.uploadFile(parts).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseHttpResponse<UploadBean>>() {
                            @Override
                            public void success(BaseHttpResponse<UploadBean> uploadBeanBaseHttpResponse) {
                                LogUtils.d("success");
                                if (uploadBeanBaseHttpResponse.data!=null){
                                    getView().uploadSuccess(uploadBeanBaseHttpResponse.data);
                                }else {
                                    getView().onError("");
                                }
                            }

                            @Override
                            public void error(int code, String message) {
                                getView().onError(message);
                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.d(e.toString());
                    }
                }).launch();

    }


    public void getDeviceListRun(int size,int page){

        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("state","");
//        paramsMap.put("latitude", LocationManager.getInstance().getLatitude()+"");
//        paramsMap.put("longitude",LocationManager.getInstance().getLongitude()+"");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        njOwnerApiService.getDeviceList(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<NjBean>>() {


            @Override
            public void success(BaseOilHttpListResponse<NjBean> baseOilHttpListResponse) {
                if(baseOilHttpListResponse!=null&&baseOilHttpListResponse.results!=null){
                    getView().showRunNj(baseOilHttpListResponse.results);
                }

            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);

            }
        });
    }

}